//
//  ListViewModel.m
//  RACDemo
//
//  Created by XFB on 2018/8/8.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "ListViewModel.h"
#import "NetworkManager.h"
#import "HttpManager.h"
#import "RecommendListItem.h"
#import "MJExtension.h"
#import "ListCellViewModel.h"
#import "RecommendListCell.h"

@interface ListViewModel () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *recomendCellVM;

@end

@implementation ListViewModel

- (RACCommand *)loadListDataCommand
{
    if (_loadListDataCommand == nil) {
        _loadListDataCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
           
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                /// 发送请求
                NSString *urString = @"https://www.yunke.com/interface/main/home";
                /// 请求参数
                NSDictionary *parameter = [NetworkManager paramWithList];
          
                [HttpManager POST:urString parameters:parameter progress:nil success:^(NSURLSessionTask *task, id responseObject) {
                    /// 数据处理
                    /// 获取推荐课程(栅栏广告)
                    NSDictionary *resultDict = responseObject[@"result"];
                    NSDictionary *recommendDict = resultDict[@"recommends"];
                    NSArray *recommends ;
                    
                    recommends = [RecommendListItem mj_objectArrayWithKeyValuesArray:recommendDict[@"courses"]];
                    /// 模型转视图模型
                    /// 创建cell视图模型 映射
                    _recomendCellVM = [[recommends.rac_sequence map:^id _Nullable(id  _Nullable value) {
                        ListCellViewModel *cellVM = [[ListCellViewModel alloc] init];
                        cellVM.item = value;
                        return cellVM;
                    }] array];
                    
                    /// 请求成功
                    [subscriber sendNext:recommends];
                    [subscriber sendCompleted];
                    
                } failure:^(NSURLSessionTask *task, NSError *error) {
                    [subscriber sendError:error];
                }];
                
                return nil;
            }];
        }];
    }
    return _loadListDataCommand;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.recomendCellVM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecommendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"listCell"];
    
    /// 视图模型
    ListCellViewModel *cellVM = self.recomendCellVM[indexPath.row];
    
    /// 绑定视图模型
    [cellVM bindViewModel:cell];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

#pragma mark --- <视图模型绑定>

- (void)bindViewModel:(UIView *)bindView
{
    UITableView *tableView = (UITableView *)bindView;
    // 注册
    [tableView registerNib:[UINib nibWithNibName:@"RecommendListCell" bundle:nil] forCellReuseIdentifier:@"listCell"];
    
    tableView.dataSource = self;
    tableView.delegate = self;
}

@end


